# -*- coding: utf-8 -*-
# File : user_service.py
# Author: taoyahui
# Date : 2021/10/17
from models.user import User
from config import db, admin_address, contract_name, contract_address, contract_abi, redis_service
from results import *
from utils import common_utils
import json


def register_impl(json_dict):
    username = json_dict['username']
    password = json_dict['password']
    address = json_dict['address']
    role = json_dict['role']
    # get_user = db.session.query(user).filter_by(username=username).all()
    get_user = User.query.filter_by(username=username).first()
    if get_user is not None:
        return exists_result

    res = common_utils.common_bc_req(admin_address, contract_name, 'createMember', [address, '','','','','',role], contract_address,
                               contract_abi)
    if res.status_code == 200:
        res_json = json.loads(res.text)
        if res_json['status'] == '0x0':
            save_user = User(username, password, address, role)
            db.session.add(save_user)
            db.session.commit()
            return success_result
        else:
            return gen_result(500, res_json['message'])
    else:
        return gen_result(res.status_code, res.text)


def login_impl(json):
    username = json['username']
    password = json['password']
    get_user = User.query.filter_by(username=username).first()
    if get_user is None:
        return empty_result
    if get_user.password == password:
        token = common_utils.get_token()
        redis_service.set(token, username)
        data = {
            'token': token,
            'username':username,
            'role': get_user.role
        }
        return gen_result(200, data)
    else:
        return user_password_error


def update_user_info(json_dict):
    username = json_dict['username']
    password = json_dict['password']
    address = json_dict['address']
    role = json_dict['role']
    get_user = User.query.filter_by(username=username).first()
    if get_user is None:
        return empty_result

    if role and address:
        res = common_utils.common_bc_req(address, 'FoodTraceNew', 'changeRole', [address, role], contract_address,
                               contract_abi)
        if res.status_code == 200:
            get_user.address = address
            get_user.role = role
        else:
            return gen_result(res.status_code, res.text)

    get_user.password = password
    db.session.add(get_user)
    db.session.commit()
    return success_result


def get_user_by_token(request):
    username = redis_service.get(request.headers.get('token'))
    if username is None:
        return {
            'code':404001,
            'data':"sername is None"
        }
    get_user = User.query.filter_by(username=username).first()
    if get_user is None:
        return {
            'code':404002,
            'data': 'User not exists'
        }
    else:
        return {
            'code':200,
            'data':get_user
        }

